{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_community.graphs import Neo4jGraph\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_community.document_loaders import PyMuPDFLoader\n",
    "from langchain_experimental.graph_transformers import LLMGraphTransformer\n",
    "from langchain_text_splitters import TokenTextSplitter\n",
    "from langchain_core.output_parsers import JsonOutputParser\n",
    "\n",
    "load_dotenv('../backend/.env')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read a document data and create chunks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "loader = PyMuPDFLoader('../data/Apple stock during pandemic.pdf')\n",
    "pages = loader.load()\n",
    "\n",
    "texts = \"\"\n",
    "for page in pages:\n",
    "    texts = texts+\" \"+page.page_content\n",
    "\n",
    "text_splitter = TokenTextSplitter(chunk_size=200, chunk_overlap=20)\n",
    "chunks = text_splitter.split_documents(pages)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize OpenAI LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name, api_key = os.environ.get('LLM_MODEL_CONFIG_openai_gpt_4o').split(\",\")\n",
    "llm = ChatOpenAI(\n",
    "            api_key=api_key,\n",
    "            model=model_name,\n",
    "            temperature=0,\n",
    "        )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate graph documents via LLMGraphTransformer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph_documents = LLMGraphTransformer(llm).convert_to_graph_documents(chunks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='Pandemic', type='Event', properties={}), Node(id='Wendy Sun', type='Person', properties={}), Node(id='Kinglee High School', type='Organization', properties={}), Node(id='Zheng Zhou', type='Location', properties={}), Node(id='He Nan', type='Location', properties={}), Node(id='China', type='Location', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Pandemic', type='Event', properties={}), type='EXPERIENCED_FLUCTUATION', properties={}), Relationship(source=Node(id='Wendy Sun', type='Person', properties={}), target=Node(id='Kinglee High School', type='Organization', properties={}), type='AFFILIATION', properties={}), Relationship(source=Node(id='Kinglee High School', type='Organization', properties={}), target=Node(id='Zheng Zhou', type='Location', properties={}), type='LOCATED_IN', properties={}), Relationship(source=Node(id='Zheng Zhou', type='Location', properties={}), target=Node(id='He Nan', type='Location', properties={}), type='LOCATED_IN', properties={}), Relationship(source=Node(id='He Nan', type='Location', properties={}), target=Node(id='China', type='Location', properties={}), type='LOCATED_IN', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 0, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\"The Changes of Apple’s Stock Price During the \\nPandemic \\nWanting Sun1,* \\n1WendySun, Kinglee High School, Zheng Zhou, He Nan, China, 450000 \\n*Corresponding author. Email: wendysun040718@163.com \\nABSTRACT \\nDuring the epidemic, the economies of various countries, including many large companies, were subject to very large \\nfluctuations and impacts. Therefore, this article will explore the ups and downs of Apple's stock price during the \\nepidemic. At different points in time and severity, Apple's stock price has risen and fallen. By observing the data on \\nYahoo, we can summarize Apple's stock price changes during this period. At the beginning of the epidemic, the global \\neconomy has been damaged, so Apple is no exception. So at the beginning of the epidemic, Apple's stock price fell\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='Stock Price', type='Concept', properties={}), Node(id='Covid-19', type='Event', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Stock Price', type='Concept', properties={}), type='INFLUENCES', properties={}), Relationship(source=Node(id='Covid-19', type='Event', properties={}), target=Node(id='Stock Price', type='Concept', properties={}), type='IMPACTS', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 0, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\", so Apple is no exception. So at the beginning of the epidemic, Apple's stock price fell \\nsharply. But later, because Apple had a strong overall strength and a good foundation, it only took three months to \\nsolve the impact of the epidemic. Later, when the epidemic returned to a serious situation, Apple's stock price was \\nimplicated and fell slightly, but within a few weeks the stock price decline was resolved and returned to normal levels, \\nand even steadily increased. Because Apple has been constantly launching new products, the epidemic will not have a \\nvery large impact on Apple's stock price. \\nKeywords: Apple, stock price, COVID-19  \\n1. INTRODUCTION \\nAs we know, the outbreak of the epidemic in 2020 \\nhas dealt a lot of blows to many countries and \\ncompanies. Population decline, massive economic \\nlosses, and national unrest. The U\")),\n",
       " GraphDocument(nodes=[Node(id='U.S. Economic Recession', type='Event', properties={}), Node(id='Euro Zone Economy', type='Economy', properties={}), Node(id='British Economy', type='Economy', properties={}), Node(id='Epidemic', type='Event', properties={}), Node(id=\"China'S Transportation Industry\", type='Industry', properties={}), Node(id=\"China'S Tourism Industry\", type='Industry', properties={}), Node(id='Chinese Economy', type='Economy', properties={}), Node(id='World Economy', type='Economy', properties={}), Node(id='Apple', type='Company', properties={})], relationships=[Relationship(source=Node(id='U.S. Economic Recession', type='Event', properties={}), target=Node(id='Serious', type='Attribute', properties={}), type='SEVERITY', properties={}), Relationship(source=Node(id='Euro Zone Economy', type='Economy', properties={}), target=Node(id='Largest Decline In Record', type='Attribute', properties={}), type='DECLINE', properties={}), Relationship(source=Node(id='British Economy', type='Economy', properties={}), target=Node(id='Largest Decline Since World War Ii', type='Attribute', properties={}), type='DECLINE', properties={}), Relationship(source=Node(id='Epidemic', type='Event', properties={}), target=Node(id=\"China'S Transportation Industry\", type='Industry', properties={}), type='IMPACT', properties={}), Relationship(source=Node(id='Epidemic', type='Event', properties={}), target=Node(id=\"China'S Tourism Industry\", type='Industry', properties={}), type='IMPACT', properties={}), Relationship(source=Node(id='Epidemic', type='Event', properties={}), target=Node(id='Chinese Economy', type='Economy', properties={}), type='IMPACT', properties={}), Relationship(source=Node(id='Chinese Economy', type='Economy', properties={}), target=Node(id='World Economy', type='Economy', properties={}), type='IMPACT', properties={}), Relationship(source=Node(id='Epidemic', type='Event', properties={}), target=Node(id='Apple', type='Company', properties={}), type='IMPACT', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 0, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\"\\ncompanies. Population decline, massive economic \\nlosses, and national unrest. The U.S. economic \\nrecession seems to be more serious than expected. The \\neuro zone economy has experienced the largest decline \\nin record, and the British economy has also suffered the \\nlargest decline since World War II. As an exogenous \\nshock, the epidemic will inevitably have a direct impact \\non China's transportation, tourism and other industries. \\nSimilarly, as China is the main source of power to \\npromote world growth, once the Chinese economy is \\naffected, the world economy will inevitably be affected. \\nThus, in such an environment where the whole world is \\naffected, to what extent have the industry giants that \\ndominate the world been hit? Let's take the most \\nfamiliar Apple company as an example, and analyze \\nhow much impact this devastating epidemic has brought \\nto Apple�\")),\n",
       " GraphDocument(nodes=[Node(id='Apple_Inc', type='Organization', properties={}), Node(id='Cupertino_California', type='Location', properties={}), Node(id='December_12_1980', type='Date', properties={}), Node(id='$623.5_Billion', type='Value', properties={}), Node(id='2012', type='Date', properties={}), Node(id='June_2014', type='Date', properties={}), Node(id='September_30_2013', type='Date', properties={}), Node(id='Hongmeng_Group', type='Organization', properties={}), Node(id='Coca-Cola', type='Organization', properties={}), Node(id='2014', type='Date', properties={}), Node(id='Google', type='Organization', properties={})], relationships=[Relationship(source=Node(id='Apple_Inc', type='Organization', properties={}), target=Node(id='Cupertino_California', type='Location', properties={}), type='HEADQUARTERED_IN', properties={}), Relationship(source=Node(id='Apple_Inc', type='Organization', properties={}), target=Node(id='December_12_1980', type='Date', properties={}), type='WENT_PUBLIC_ON', properties={}), Relationship(source=Node(id='Apple_Inc', type='Organization', properties={}), target=Node(id='$623.5_Billion', type='Value', properties={}), type='MARKET_VALUE', properties={}), Relationship(source=Node(id='$623.5_Billion', type='Value', properties={}), target=Node(id='2012', type='Date', properties={}), type='RECORDED_IN', properties={}), Relationship(source=Node(id='Apple_Inc', type='Organization', properties={}), target=Node(id='June_2014', type='Date', properties={}), type='LARGEST_COMPANY_BY_MARKET_CAPITALIZATION', properties={}), Relationship(source=Node(id='Apple_Inc', type='Organization', properties={}), target=Node(id='September_30_2013', type='Date', properties={}), type='MOST_VALUABLE_BRAND', properties={}), Relationship(source=Node(id='Hongmeng_Group', type='Organization', properties={}), target=Node(id='Coca-Cola', type='Organization', properties={}), type='SURPASSED_BY', properties={}), Relationship(source=Node(id='Apple_Inc', type='Organization', properties={}), target=Node(id='2014', type='Date', properties={}), type='SURPASSED_GOOGLE', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 0, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=' an example, and analyze \\nhow much impact this devastating epidemic has brought \\nto Apple’s stocks in several aspects. \\n \\n \\n2. BACKGROUND INFORMATION OF \\nAPPLE COMPANY \\nApple Inc. is an American multinational technology \\ncompany that specializes in consumer electronics, \\ncomputer software, and online services. In it’s \\nheadquartered in Cupertino, California. Apple went \\npublic on December 12, 1980, and set a record of \\n$623.5 billion in market value in 2012. As of June \\n2014, Apple has become the world\\'s largest company by \\nmarket capitalization for three consecutive years. On \\nSeptember 30, 2013, in the Hongmeng Group\\'s \"Best \\nGlobal Brand\" report, Apple surpassed Coca-Cola to \\nbecome the world\\'s most valuable brand. In 2014, the \\nApple brand surpassed Google to become the world\\'s')),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='Google', type='Organization', properties={}), Node(id='Fortune Global 500', type='List', properties={}), Node(id='Technology Company', type='Industry', properties={}), Node(id='Pc Vendor', type='Industry', properties={}), Node(id='Smartphone Manufacturer', type='Industry', properties={}), Node(id='Us$2 Trillion', type='Value', properties={}), Node(id='2021 3Rd International Conference On Economic Management And Cultural Industry', type='Event', properties={}), Node(id='Atlantis Press International B.V.', type='Organization', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Google', type='Organization', properties={}), type='SURPASSED', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Us$2 Trillion', type='Value', properties={}), type='MARKET_VALUE_EXCEEDED', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Fortune Global 500', type='List', properties={}), type='RANKED_6TH_ON', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Technology Company', type='Industry', properties={}), type='LARGEST_BY_REVENUE', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Pc Vendor', type='Industry', properties={}), type='FOURTH-LARGEST_BY_UNIT_SALES', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Smartphone Manufacturer', type='Industry', properties={}), type='FOURTH-LARGEST', properties={}), Relationship(source=Node(id='2021 3Rd International Conference On Economic Management And Cultural Industry', type='Event', properties={}), target=Node(id='Atlantis Press International B.V.', type='Organization', properties={}), type='PUBLISHED_BY', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 0, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\"'s most valuable brand. In 2014, the \\nApple brand surpassed Google to become the world's \\nmost valuable brand. On August 19, 2020 local time, \\nApple's market value exceeded US$2 trillion for the \\nfirst time. Ranked 6th on the Fortune Global 500 list in \\n2021. Apple is the world's largest technology company \\nby revenue and, since January 2021, the world's most \\nvaluable company. As of 2021, Apple is the world's \\nfourth-largest PC vendor by unit sales, and fourth-\\nlargest smartphone manufacturer. \\n \\nAdvances in Economics, Business and Management Research, volume 203\\nProceedings of the 2021 3rd International Conference on Economic Management and\\nCultural Industry (ICEMCI 2021)\\nCopyright © 2021 The Authors. Published by Atlantis Press International B.V.\\nThis is an open access article distributed under the CC BY-NC 4.0 license -http\")),\n",
       " GraphDocument(nodes=[Node(id='Cc By-Nc 4.0 License', type='License', properties={}), Node(id='Http://Creativecommons.Org/Licenses/By-Nc/4.0/', type='Url', properties={})], relationships=[Relationship(source=Node(id='Cc By-Nc 4.0 License', type='License', properties={}), target=Node(id='Http://Creativecommons.Org/Licenses/By-Nc/4.0/', type='Url', properties={}), type='DISTRIBUTED_UNDER', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 0, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content='\\nThis is an open access article distributed under the CC BY-NC 4.0 license -http://creativecommons.org/licenses/by-nc/4.0/.\\n373\\n')),\n",
       " GraphDocument(nodes=[Node(id='Population', type='Concept', properties={}), Node(id='Spending', type='Concept', properties={}), Node(id='Inventories', type='Concept', properties={}), Node(id='Industries', type='Concept', properties={}), Node(id='Global Economy', type='Concept', properties={}), Node(id='International Monetary Fund', type='Organization', properties={}), Node(id='April 2020 Global Economic Survey', type='Publication', properties={}), Node(id='New Crown Epidemic', type='Event', properties={}), Node(id='2008-2009 Financial Crisis', type='Event', properties={}), Node(id='Experts', type='Person', properties={}), Node(id='Global Economic Recession', type='Event', properties={}), Node(id='End Of 2007', type='Date', properties={}), Node(id='June 2009', type='Date', properties={}), Node(id='Apple', type='Organization', properties={}), Node(id='China', type='Location', properties={})], relationships=[Relationship(source=Node(id='Population', type='Concept', properties={}), target=Node(id='Industries', type='Concept', properties={}), type='DECLINE_CAUSES_STAGNATION', properties={}), Relationship(source=Node(id='Spending', type='Concept', properties={}), target=Node(id='Industries', type='Concept', properties={}), type='DECREASE_CAUSES_STAGNATION', properties={}), Relationship(source=Node(id='Inventories', type='Concept', properties={}), target=Node(id='Industries', type='Concept', properties={}), type='INCREASE_CAUSES_STAGNATION', properties={}), Relationship(source=Node(id='Industries', type='Concept', properties={}), target=Node(id='Global Economy', type='Concept', properties={}), type='STAGNATION_IMPACTS', properties={}), Relationship(source=Node(id='International Monetary Fund', type='Organization', properties={}), target=Node(id='April 2020 Global Economic Survey', type='Publication', properties={}), type='STATED_IN', properties={}), Relationship(source=Node(id='New Crown Epidemic', type='Event', properties={}), target=Node(id='Global Economy', type='Concept', properties={}), type='CAUSES_SHRINKAGE', properties={}), Relationship(source=Node(id='Global Economy', type='Concept', properties={}), target=Node(id='2008-2009 Financial Crisis', type='Event', properties={}), type='MORE_SEVERE_THAN', properties={}), Relationship(source=Node(id='Experts', type='Person', properties={}), target=Node(id='Economic Contraction', type='Concept', properties={}), type='PREDICTED_IMPACT', properties={}), Relationship(source=Node(id='Global Economic Recession', type='Event', properties={}), target=Node(id='End Of 2007', type='Date', properties={}), type='BEGAN', properties={}), Relationship(source=Node(id='Global Economic Recession', type='Event', properties={}), target=Node(id='June 2009', type='Date', properties={}), type='RECOVERED', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='China', type='Location', properties={}), type='COMPONENTS_PRODUCED_IN', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 1, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content='3. MACRO PERSPECTIVE  \\nFrom a macro perspective, the decline in population, \\nthe decrease in spending, and the increase in inventories \\nhave caused many industries to stagnate. This is bound \\nto bring a huge blow on the global economy to a serious \\nextent. The International Monetary Fund stated in the \\n\"April 2020 Global Economic Survey\" that due to the \\nnew crown epidemic, the global economy is expected to \\nshrink by 3% in 2020, and the impact will be much \\nmore severe than the 2008-2009 financial crisis. Some \\nexperts have predicted that the impact of the epidemic \\ncrisis on economic contraction will be three times that \\nof the financial crisis. The last global economic \\nrecession began at the end of 2007 and did not fully \\nrecover until June 2009. Most of Apple\\'s components \\nare produced in China, so when China is')),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='China', type='Location', properties={}), Node(id='Epidemic', type='Event', properties={}), Node(id='Wall Street Journal', type='Organization', properties={}), Node(id='March 24, 2020', type='Date', properties={}), Node(id='Microsoft', type='Organization', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='China', type='Location', properties={}), type='COMPONENTS_PRODUCED_IN', properties={}), Relationship(source=Node(id='China', type='Location', properties={}), target=Node(id='Epidemic', type='Event', properties={}), type='AFFECTED_BY', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Epidemic', type='Event', properties={}), type='AFFECTED_BY', properties={}), Relationship(source=Node(id='Wall Street Journal', type='Organization', properties={}), target=Node(id='March 24, 2020', type='Date', properties={}), type='REPORTED_ON', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Microsoft', type='Organization', properties={}), type='FELL_OUT_OF_TRILLION_CLUB', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 1, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=' until June 2009. Most of Apple\\'s components \\nare produced in China, so when China is affected by the \\nepidemic and the work is stopped, Apple\\'s products \\ncannot be made at the same time [3]. This will \\ninevitably have a huge impact on Apple. According to \\ninformation reported by the Wall Street Journal on \\nMarch 24, 2020, Apple\\'s stock price closed down 2.12% \\non Monday, and its market value fell below US$1 \\ntrillion to US$981.7 billion. In this way, Apple fell out \\nof the \"trillion club\" of US stocks, of which only \\nMicrosoft was left [7]. \\n4. MICRO PERSPECTIVE \\nIt is obvious that Apple has been affected by the \\nepidemic in a great extent, however, we still need to \\ntake a look Apple’s economic changes')),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='February 3, 2020', type='Date', properties={}), Node(id='March 23, 2020', type='Date', properties={}), Node(id='September 1, 2020', type='Date', properties={})], relationships=[Relationship(source=Node(id='February 3, 2020', type='Date', properties={}), target=Node(id='Apple', type='Organization', properties={}), type='OPENING_PRICE_76.07', properties={}), Relationship(source=Node(id='February 3, 2020', type='Date', properties={}), target=Node(id='Apple', type='Organization', properties={}), type='CLOSING_PRICE_80.01', properties={}), Relationship(source=Node(id='March 23, 2020', type='Date', properties={}), target=Node(id='Apple', type='Organization', properties={}), type='OPENING_PRICE_57.02', properties={}), Relationship(source=Node(id='March 23, 2020', type='Date', properties={}), target=Node(id='Apple', type='Organization', properties={}), type='CLOSING_PRICE_56.09', properties={}), Relationship(source=Node(id='September 1, 2020', type='Date', properties={}), target=Node(id='Apple', type='Organization', properties={}), type='OPENING_PRICE_132.76', properties={}), Relationship(source=Node(id='September 1, 2020', type='Date', properties={}), target=Node(id='Apple', type='Organization', properties={}), type='CLOSING_PRICE_134.18', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 1, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=' great extent, however, we still need to \\ntake a look Apple’s economic changes in a micro \\nperspective. At the beginning of the epidemic, which is \\nthe start of February, the price started to decline. On \\nFebruary 3, 2020, the opening price is 76.07; the \\nclosing price is 80.01. From this point, the closing price \\nstarted to diminish. On March 23, 2020, the opening \\nprice is 57.02, and the closing price is 56.09. The stock \\nprice for this day is the lowest price during epidemic in \\nApple. Then, it started to increase gradually until \\nSeptember 1, this day is also a peak, the opening price \\non September 1 is 132.76 and the closing price is \\n134.18. However, after September 1, the price started to \\ndrop again. There is a very low price on')),\n",
       " GraphDocument(nodes=[Node(id='September 1', type='Date', properties={}), Node(id='September 18, 2020', type='Date', properties={}), Node(id='110.40', type='Price', properties={}), Node(id='106.84', type='Price', properties={}), Node(id=\"Apple'S Stock Price\", type='Stock', properties={}), Node(id='Advances In Economics, Business And Management Research, Volume 203', type='Publication', properties={})], relationships=[Relationship(source=Node(id='September 1', type='Date', properties={}), target=Node(id=\"Apple'S Stock Price\", type='Stock', properties={}), type='PRICE_DROP_START', properties={}), Relationship(source=Node(id='September 18, 2020', type='Date', properties={}), target=Node(id=\"Apple'S Stock Price\", type='Stock', properties={}), type='LOW_PRICE_DATE', properties={}), Relationship(source=Node(id='110.40', type='Price', properties={}), target=Node(id=\"Apple'S Stock Price\", type='Stock', properties={}), type='OPENING_PRICE', properties={}), Relationship(source=Node(id='106.84', type='Price', properties={}), target=Node(id=\"Apple'S Stock Price\", type='Stock', properties={}), type='CLOSING_PRICE', properties={}), Relationship(source=Node(id=\"Apple'S Stock Price\", type='Stock', properties={}), target=Node(id='Advances In Economics, Business And Management Research, Volume 203', type='Publication', properties={}), type='MENTIONED_IN', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 1, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\" after September 1, the price started to \\ndrop again. There is a very low price on September 18, \\n2020. The opening price is 110.40, the closing price is \\n106.84.  \\n \\nFigure 1 Apple's stock price \\n \\n \\nAdvances in Economics, Business and Management Research, volume 203\\n374\\n\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='Iphone 12', type='Product', properties={}), Node(id='Iphone 12 Pro', type='Product', properties={}), Node(id='Iphone 12 Pro Max', type='Product', properties={}), Node(id='October 14, 2020', type='Date', properties={}), Node(id='October 12, 2020', type='Date', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Iphone 12', type='Product', properties={}), type='ANNOUNCED', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Iphone 12 Pro', type='Product', properties={}), type='ANNOUNCED', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Iphone 12 Pro Max', type='Product', properties={}), type='ANNOUNCED', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='October 14, 2020', type='Date', properties={}), type='ANNOUNCEMENT_DATE', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='October 12, 2020', type='Date', properties={}), type='STOCK_PRICE_INCREASE', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 2, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\" \\nFigure 2. Apple's stock price \\nThe reasons which caused this drop might be a lot, \\nthe most common answer might be the delay of the \\nannouncement of new Apple Products. A numbers of \\npeople waiting on this day for a long time, because \\nApple supposed to announce the new products like \\niPhone 12, iPhone 12 Pro and iPhone 12 Pro Max, etc. \\nNevertheless, Apple delayed the publish of their new \\nproducts until October 14, 2020. There is a small peak \\nof rising stock price on October 12, which is two days \\nbefore the publish of the new products. The increasing \\nof Apple stock might because people putted huge \\nexpectations on these new products, and they have been \\nwaited for a long period. This motivation lead them to \\nbuy these products right away, which caused the \\nsuddenly increasing price of Apple’ stock. On\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Company', properties={}), Node(id='November 2, 2020', type='Date', properties={}), Node(id='December 28, 2020', type='Date', properties={}), Node(id='January 26, 2021', type='Date', properties={})], relationships=[Relationship(source=Node(id='November 2, 2020', type='Date', properties={}), target=Node(id='Apple', type='Company', properties={}), type='STOCK_PRICE_DROP', properties={}), Relationship(source=Node(id='December 28, 2020', type='Date', properties={}), target=Node(id='Apple', type='Company', properties={}), type='STOCK_PRICE_INCREASE', properties={}), Relationship(source=Node(id='January 26, 2021', type='Date', properties={}), target=Node(id='Apple', type='Company', properties={}), type='STOCK_PRICE_PEAK', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 2, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\" products right away, which caused the \\nsuddenly increasing price of Apple’ stock. On \\nNovember 2, there is a small drop on this day. The \\nopening price is 109.11, and the closing price is 108.77. \\nOn December 28, there is another increasing peak on \\nApple’s stock. The opening price is 133.99, and the \\nclosing price 136.69. This is also the highest price of \\nApple stock in 2020. After 2020, Apple's stock price \\nwill continue to increase in 2021. This study used two \\nyears of data for comparison, which means it will only \\nuse data from February 3, 2021 and before. So at the \\nbeginning of 2021, Apple’s stock price ushered in its \\nfirst peak. On January 26, 2021, the opening price is \\n143.6, and the closing price is 143.16.\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='Microsoft', type='Organization', properties={}), Node(id='Bill Gates', type='Person', properties={}), Node(id='Paul Allen', type='Person', properties={}), Node(id='April 4, 1975', type='Date', properties={}), Node(id='Redmond, Washington', type='Location', properties={})], relationships=[Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Bill Gates', type='Person', properties={}), type='FOUNDED_BY', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Paul Allen', type='Person', properties={}), type='FOUNDED_BY', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='April 4, 1975', type='Date', properties={}), type='FOUNDED_ON', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Redmond, Washington', type='Location', properties={}), type='HEADQUARTERED_IN', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 2, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=', the opening price is \\n143.6, and the closing price is 143.16. Later on, there is \\na small drop until February 3, 2021. The opening price \\nis 135.76, and the closing price is 133.94. Comparing \\nthe data at the same day but different year on February \\n3, we can see that the price of Apple’s stock increased \\nby 67.4% in one year. \\n5. COMPARISON WITH MICROSOFT \\nCOMPANY \\nAnother very famous company which can compete \\nwith Apple is Microsoft Company. Microsoft is an \\nAmerican multinational technology company founded \\nby Bill Gates and Paul Allen on April 4, 1975. The \\ncompany is headquartered in Redmond, Washington \\n(near Seattle), focusing on R&D, manufacturing, \\nlicensing and providing a wide range of computer \\nsoftware services. The most famous')),\n",
       " GraphDocument(nodes=[Node(id='Microsoft', type='Organization', properties={}), Node(id='Windows Operating System', type='Product', properties={}), Node(id='Office Series Software', type='Product', properties={}), Node(id='Apple', type='Organization', properties={})], relationships=[Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Windows Operating System', type='Product', properties={}), type='PRODUCES', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Office Series Software', type='Product', properties={}), type='PRODUCES', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Apple', type='Organization', properties={}), type='COMPARED_TO', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 2, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\", \\nlicensing and providing a wide range of computer \\nsoftware services. The most famous and best-selling \\nproducts are Windows operating system and Office \\nseries software. It is the world's largest computer \\nsoftware provider and the leader of PC (Personal \\nComputer) software development in the world. On June \\n23, 2021, Microsoft's market value exceeded the $2 \\ntrillion mark. This is the second US company to enter \\nafter Apple's breakthrough of $2 trillion. In 2019, the \\nmarket value of Microsoft reached US$1 trillion for the \\nfirst time, 33 years after the original Microsoft IPO. \\nFrom 1 trillion to 2 trillion, Microsoft only took about 2 \\nyears. Comparing Microsoft and Apple, Microsoft has \\nmore patents, company environment, R&D ratio, and \\nR&D investment than Apple. Apple's profit, profit \\nmargin, revenue, etc. are\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='Microsoft', type='Organization', properties={}), Node(id='2_Trillion', type='Value', properties={}), Node(id='Profit', type='Concept', properties={}), Node(id='Profit_Margin', type='Concept', properties={}), Node(id='Revenue', type='Concept', properties={}), Node(id='Market_Value', type='Concept', properties={}), Node(id='Epidemic', type='Event', properties={}), Node(id='Employees', type='Group', properties={}), Node(id='Home', type='Location', properties={}), Node(id='Stock_Price', type='Concept', properties={}), Node(id='March_6_2020', type='Date', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Microsoft', type='Organization', properties={}), type='MORE_R&D_INVESTMENT_THAN', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Microsoft', type='Organization', properties={}), type='MORE_PROFIT_THAN', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Microsoft', type='Organization', properties={}), type='MORE_PROFIT_MARGIN_THAN', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Microsoft', type='Organization', properties={}), type='MORE_REVENUE_THAN', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='2_Trillion', type='Value', properties={}), type='MARKET_VALUE_CAN_BREAK', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Profit', type='Concept', properties={}), type='NOT_GOOD_PERFORMANCE_IN', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Profit_Margin', type='Concept', properties={}), type='NOT_GOOD_PERFORMANCE_IN', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Enterprise-Level_Market', type='Concept', properties={}), type='IS_IN', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Epidemic', type='Event', properties={}), type='AFFECTED_BY', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Employees', type='Group', properties={}), type='LET_WORK_AT_HOME', properties={}), Relationship(source=Node(id='Employees', type='Group', properties={}), target=Node(id='Home', type='Location', properties={}), type='WORK_AT', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='Stock_Price', type='Concept', properties={}), type='CHANGED_WITH_EPIDEMIC', properties={}), Relationship(source=Node(id='Epidemic', type='Event', properties={}), target=Node(id='March_6_2020', type='Date', properties={}), type='REPORT_DATE', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 2, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\"&D investment than Apple. Apple's profit, profit \\nmargin, revenue, etc. are more than that of Microsoft. \\nThis is why Apple’s market value can break 2 trillion \\nearlier than Microsoft. On the two items of profit and \\nprofit margin, Microsoft's performance is not good. \\nAfter all, it is an enterprise-level market. If the profit \\nmargin is too high, customers are easy to lose. \\nDuring the epidemic, Microsoft let their employees \\nto work at home, Its stock price has also changed more \\nor less with the epidemic flow. In March 6, 2020 report, \\nAdvances in Economics, Business and Management Research, volume 203\\n375\\n\")),\n",
       " GraphDocument(nodes=[Node(id='Microsoft', type='Organization', properties={}), Node(id='April 24, 2000', type='Date', properties={}), Node(id='February 10, 2020', type='Date', properties={}), Node(id='March 16, 2020', type='Date', properties={}), Node(id='March 23, 2020', type='Date', properties={}), Node(id='Apple', type='Organization', properties={}), Node(id='Dow', type='Index', properties={})], relationships=[Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='14.74%', type='Percentage', properties={}), type='STOCK_PRICE_DROP', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='April 24, 2000', type='Date', properties={}), type='LARGEST_SINGLE_DAY_DROP_SINCE', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='15.6%', type='Percentage', properties={}), type='STOCK_PRICE_DROP', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='27%', type='Percentage', properties={}), type='STOCK_PRICE_DROP_PAST_MONTH', properties={}), Relationship(source=Node(id='Dow', type='Index', properties={}), target=Node(id='31%', type='Percentage', properties={}), type='STOCK_PRICE_DROP_PAST_MONTH', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='February 10, 2020', type='Date', properties={}), type='STOCK_PRICE_START_DECREASE', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='March 16, 2020', type='Date', properties={}), type='LOWEST_PRICE_DATE', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='140.0', type='Price', properties={}), type='OPENING_PRICE', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='135.42', type='Price', properties={}), type='CLOSING_PRICE', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='March 23, 2020', type='Date', properties={}), type='LOWEST_PRICE_DATE', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 3, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\"Microsoft's stock price plummeted 14.74% on Monday, \\nthe largest single-day drop since April 24, 2000. At that \\ntime, Microsoft's stock price fell by 15.6%. In the past \\nmonth, Microsoft's stock price has fallen by 27%, and \\nthe Dow has fallen by 31% [5]. \\nIn a micro perspective, during epidemic time, \\nMicrosoft’s stock start decrease since February 10, \\n2020. At the time when Apple had their its lowest price \\non March 23, Microsoft also having a bad situation, but \\nthe lowest price of Microsoft price is on March 16. The \\nopening price on that day is 140.0, the closing price is \\n135.42. Even thought the stock price of Microsoft is \\nmuch higher than Apple, but their volume is quite \\nsimilar, even Apple’s volumes are higher than \\nMicrosoft. It is because Apple\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Company', properties={}), Node(id='Microsoft', type='Company', properties={}), Node(id='Epidemic', type='Event', properties={}), Node(id=\"Apple'S Stock Price\", type='Financial_metric', properties={}), Node(id=\"Apple'S New Products\", type='Product', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Company', properties={}), target=Node(id='Microsoft', type='Company', properties={}), type='HIGHER_MARKET_VALUE_THAN', properties={}), Relationship(source=Node(id='Epidemic', type='Event', properties={}), target=Node(id=\"Apple'S Stock Price\", type='Financial_metric', properties={}), type='AFFECTED', properties={}), Relationship(source=Node(id=\"Apple'S Stock Price\", type='Financial_metric', properties={}), target=Node(id='Apple', type='Company', properties={}), type='RECOVERED_QUICKLY', properties={}), Relationship(source=Node(id=\"Apple'S Stock Price\", type='Financial_metric', properties={}), target=Node(id='Apple', type='Company', properties={}), type='CONTINUED_TO_RISE', properties={}), Relationship(source=Node(id=\"Apple'S New Products\", type='Product', properties={}), target=Node(id=\"Apple'S Stock Price\", type='Financial_metric', properties={}), type='IMPACTED', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 3, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\"\\nsimilar, even Apple’s volumes are higher than \\nMicrosoft. It is because Apple's market value is higher \\nthan Microsoft. \\n6. OVERALL TREND \\nFor Apple, the epidemic only affected it at the \\nbeginning, but it took only four months for Apple to \\nreturn to its previous normal stock price. It can be seen \\nthat the true hidden strength behind Apple can enable \\nApple. Recovered in such a short time. The overall \\nsituation of Apple's stock price continued to rise. It only \\nfell sharply between February and June, when the \\nepidemic first began, but it quickly returned to its \\noriginal state. During the period from August to \\nNovember, the impact of Apple's new products rose \\nfirst and then fell back to normal levels. There was also \\na peak in early 2021, and then it returned to normal \\nlevels. The overall\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='Microsoft', type='Organization', properties={}), Node(id='Early 2021', type='Time', properties={}), Node(id='December 2020', type='Time', properties={}), Node(id='September', type='Time', properties={}), Node(id='February', type='Time', properties={}), Node(id='March', type='Time', properties={}), Node(id='June', type='Time', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Early 2021', type='Time', properties={}), type='PEAK', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='67.4%', type='Percentage', properties={}), type='INCREASE', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='February', type='Time', properties={}), type='DECLINE_START', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='March', type='Time', properties={}), type='DECLINE_END', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='June', type='Time', properties={}), type='RECOVERY', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='December 2020', type='Time', properties={}), type='HIGHEST_VALUE', properties={}), Relationship(source=Node(id='Microsoft', type='Organization', properties={}), target=Node(id='September', type='Time', properties={}), type='HIGHEST_VALUE', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 3, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\" \\na peak in early 2021, and then it returned to normal \\nlevels. The overall level of Apple's stock increased by \\n67.4% over the same period. \\nFor Microsoft, his situation is actually similar to that \\nof the same type of Apple. At the beginning of the \\nepidemic, there was a huge decline from February to \\nMarch, but it was basically fully restored to the \\nprevious level before June. Like Apple, it took just four \\nmonths to solve the severe damage caused by the \\nepidemic, which shows that Microsoft's strength cannot \\nbe underestimated. Microsoft's overall trend continues \\nto rise. However, unlike Apple, the highest value of \\nApple's stock is in December, which is the end of 2020, \\nbut the highest value of Microsoft's stock is in \\nSeptember. After September, the stock declined \\nslightly, but it only fell\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Company', properties={}), Node(id='Microsoft', type='Company', properties={}), Node(id='Pandemic', type='Event', properties={})], relationships=[Relationship(source=Node(id='Microsoft', type='Company', properties={}), target=Node(id='Apple', type='Company', properties={}), type='HIGHER_STOCK_PRICE_THAN', properties={}), Relationship(source=Node(id='Apple', type='Company', properties={}), target=Node(id='Microsoft', type='Company', properties={}), type='HIGHER_TRANSACTION_VOLUME_THAN', properties={}), Relationship(source=Node(id='Pandemic', type='Event', properties={}), target=Node(id='Apple', type='Company', properties={}), type='DID_NOT_CAUSE_IRREPARABLE_BLOW_TO', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 3, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\" in \\nSeptember. After September, the stock declined \\nslightly, but it only fell to a normal level, and then \\nslowly climbed up.  \\n7. CONCLUSION \\nComparing Apple's stock price and Microsoft's \\nstock price, we can find that Microsoft's stock price is \\nmuch higher than Apple's, but when we compare their \\nnumber of transactions, we find that Apple's number of \\ntransactions is many times greater than that of \\nMicrosoft. This has also led to the fact that the total \\ntrading volume of Apple stock and Microsoft stock is \\nsimilar, and sometimes Apple is even higher than \\nMicrosoft.  \\nTo conclude, the pandemic did not bring an \\nirreparable blow to Apple. Instead, it only took it four \\nmonths to restore its stock to its original value, or even \\nrise. This is enough to show that Apple's\")),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Organization', properties={}), Node(id='Yahoo', type='Organization', properties={}), Node(id='Verizon Media', type='Organization', properties={}), Node(id='Ziemba, W. T.', type='Person', properties={}), Node(id='Aapl', type='Stock', properties={}), Node(id='Covid-19', type='Event', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Aapl', type='Stock', properties={}), type='HAS_STOCK', properties={}), Relationship(source=Node(id='Yahoo', type='Organization', properties={}), target=Node(id='Verizon Media', type='Organization', properties={}), type='PART_OF', properties={}), Relationship(source=Node(id='Ziemba, W. T.', type='Person', properties={}), target=Node(id='Covid-19', type='Event', properties={}), type='WROTE_ABOUT', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 3, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=\" stock to its original value, or even \\nrise. This is enough to show that Apple's foundation is \\nstable and there are excellent strategies to enable them \\nto recover their losses in time when they faced strong \\nimpacts. In general, Apple's stock only declined during \\nthe most severe period of the pandemic, and remained \\nat a stable value for the rest of the time, and there was \\nalso a phenomenon of continuous rise. \\nREFERENCES \\n[1] Yahoo is now a part of Verizon Media. (n.d.). \\nYahoo. \\nRetrieved \\nAugust \\n29, \\n2021, \\nfrom \\nhttps://finance.yahoo.com/quote/AAPL/chart?\\\\ \\n[2] Ziemba, W. T. (2020, June 21). The COVID-19 \\nCrash in the US \\nStock Market\")),\n",
       " GraphDocument(nodes=[Node(id='Ziemba', type='Person', properties={}), Node(id='Yan', type='Person', properties={}), Node(id='Camba', type='Person', properties={}), Node(id='The Covid-19 Crash In The Us Stock Market', type='Publication', properties={}), Node(id='Analysis Of The Effect Of Covid-19 On The Stock Market And Investing Strategies', type='Publication', properties={}), Node(id='The Effects Of Restrictions In Economic Activity On The Spread Of Covid-19 In The Philippines', type='Publication', properties={})], relationships=[Relationship(source=Node(id='Ziemba', type='Person', properties={}), target=Node(id='The Covid-19 Crash In The Us Stock Market', type='Publication', properties={}), type='AUTHOR', properties={}), Relationship(source=Node(id='Yan', type='Person', properties={}), target=Node(id='Analysis Of The Effect Of Covid-19 On The Stock Market And Investing Strategies', type='Publication', properties={}), type='AUTHOR', properties={}), Relationship(source=Node(id='Camba', type='Person', properties={}), target=Node(id='The Effects Of Restrictions In Economic Activity On The Spread Of Covid-19 In The Philippines', type='Publication', properties={}), type='AUTHOR', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 3, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content='2020, June 21). The COVID-19 \\nCrash in the US \\nStock Market. \\nZiemba. \\nhttps://papers.ssrn.com/sol3/papers.cfm?abstract_i\\nd=3632410 \\n[3] Yan, B. (2020, March 28). Analysis of the Effect of \\nCOVID-19 on the Stock Market and Investing \\nStrategies. \\nYan. \\nhttps://papers.ssrn.com/sol3/Papers.cfm?abstract_i\\nd=3563380 \\n[4] Camba, J. A. C. (2020). The Effects of Restrictions \\nin Economic Activity on the Spread of COVID-19 \\nin the Philippines: Insights from Apple and Google \\nMobility Indicators -The Journal of Asian Finance, \\nEconomics and Business | Korea Science. Camb')),\n",
       " GraphDocument(nodes=[Node(id='The Journal Of Asian Finance, Economics And Business', type='Publication', properties={}), Node(id='Korea Science', type='Organization', properties={}), Node(id='Camba', type='Person', properties={}), Node(id='Shivaani, M. V.', type='Person', properties={}), Node(id='Journal Of Emerging Technologies In Accounting', type='Publication', properties={}), Node(id='Apple', type='Organization', properties={}), Node(id='Amazon', type='Organization', properties={})], relationships=[Relationship(source=Node(id='The Journal Of Asian Finance, Economics And Business', type='Publication', properties={}), target=Node(id='Korea Science', type='Organization', properties={}), type='PUBLISHED_BY', properties={}), Relationship(source=Node(id='Camba', type='Person', properties={}), target=Node(id='The Journal Of Asian Finance, Economics And Business', type='Publication', properties={}), type='AUTHOR', properties={}), Relationship(source=Node(id='Shivaani, M. V.', type='Person', properties={}), target=Node(id='Journal Of Emerging Technologies In Accounting', type='Publication', properties={}), type='AUTHOR', properties={}), Relationship(source=Node(id='Apple', type='Organization', properties={}), target=Node(id='Amazon', type='Organization', properties={}), type='COMPARED_TO', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 3, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content=' Indicators -The Journal of Asian Finance, \\nEconomics and Business | Korea Science. Camba. \\nhttps://www.koreascience.or.kr/article/JAKO2020\\n34651879125.page \\n[5] Shivaani, M. V. (n.d.). Comparing Apple to \\nAmazon: Just a Matter of Words in Machine \\nLearning \\nWorld \\n| \\nJournal \\nof \\nEmerging \\nTechnologies in Accounting. Shivaani. Retrieved \\nAugust \\n29, \\n2021, \\nfrom \\nhttps://meridian.allenpress.com/jeta/article-\\nabstract/doi/10.2308/JETA-2020-\\n045/464597/Comparing-Apple-to-Amazon-Just-a-\\nMatter-of-Words \\n[6] Apple (AAPL)')),\n",
       " GraphDocument(nodes=[Node(id='Apple', type='Company', properties={}), Node(id='Microsoft', type='Company', properties={}), Node(id='Trillion-Dollar Club', type='Economic status', properties={})], relationships=[Relationship(source=Node(id='Apple', type='Company', properties={}), target=Node(id='Trillion-Dollar Club', type='Economic status', properties={}), type='FALLEN_BELOW', properties={}), Relationship(source=Node(id='Microsoft', type='Company', properties={}), target=Node(id='Trillion-Dollar Club', type='Economic status', properties={}), type='MEMBER', properties={})], source=Document(metadata={'source': '../data/Apple stock during pandemic.pdf', 'file_path': '../data/Apple stock during pandemic.pdf', 'page': 3, 'total_pages': 4, 'format': 'PDF 1.5', 'title': '', 'author': 'EDZ', 'subject': '', 'keywords': '', 'creator': 'Microsoft® Word 2016', 'producer': 'Microsoft® Word 2016; modified using iText 5.0.6 (c) 1T3XT BVBA', 'creationDate': \"D:20211208111846+05'30'\", 'modDate': \"D:20220105193619+05'30'\", 'trapped': ''}, page_content='a-\\nMatter-of-Words \\n[6] Apple (AAPL) Vs Microsoft (MSFT): Which Is A \\nBetter Tech Stock To Buy Right Now?, 28 Jan. \\n2021, \\nwww.nasdaq.com/articles/apple-aapl-vs-\\nmicrosoft-msft%3A-which-is-a-better-tech-stock-\\nto-buy-right-now-2021-01-28. \\n[7] Osipovich Alexander. “Apple’s market capitalisation \\nhas fallen below trillion, and Microsoft is the only \\nremaining U.S. company in the trillion-dollar \\nclub.” Wall Street News, 24 Mar. 2020, \\ncn.wsj.com/articles/  \\nAdvances in Economics, Business and Management Research, volume 203\\n376\\n'))]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph_documents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add nodes and relations to Neo4j DB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = Neo4jGraph(url=os.environ.get('NEO4J_URI'), \n",
    "                    database=\"neo4j\", \n",
    "                    username=os.environ.get('NEO4J_USERNAME'), \n",
    "                    password=os.environ.get('NEO4J_PASSWORD'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph.add_graph_documents(graph_documents)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get distinct nodes and relations from graph document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "node_labels = set()\n",
    "rel_labels = set()\n",
    "\n",
    "for gd in graph_documents:\n",
    "    for node in gd.nodes:\n",
    "        node_labels.add(node.type)\n",
    "    \n",
    "    for rel in gd.relationships:\n",
    "            rel_labels.add(rel.type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "system_prompt = \"\"\"Please consolidate the following list of types into a smaller set of more general, semantically \n",
    "related types. The consolidated types must be drawn from the original list; do not introduce new types.  \n",
    "Return a JSON object representing the mapping of original types to consolidated types. Every key is the consolidated type\n",
    "and value is list of the original types that were merged into the consolidated type. Prioritize using the most generic and \n",
    "repeated term when merging. If a type doesn't merge with any other type, it should still be included in the output, \n",
    "mapped to itself.\n",
    "\n",
    "**Input:** A list of strings representing the types to be consolidated. These types may represent either node \n",
    "labels or relationship labels Your algorithm should do appropriate groupings based on semantic similarity.\n",
    "\n",
    "Example 1:\n",
    "Input: \n",
    "[ \"Person\", \"Human\", \"People\", \"Company\", \"Organization\", \"Product\"]\n",
    "Output :\n",
    "[Person\": [\"Person\", \"Human\", \"People\"], Organization\": [\"Company\", \"Organization\"], Product\": [\"Product\"]]\n",
    "\n",
    "Example 2:\n",
    "Input :\n",
    "[\"CREATED_FOR\", \"CREATED_TO\", \"CREATED\", \"PLACE\", \"LOCATION\", \"VENUE\"]\n",
    "Output:\n",
    "[\"CREATED\": [\"CREATED_FOR\", \"CREATED_TO\", \"CREATED\"],\"PLACE\": [\"PLACE\", \"LOCATION\", \"VENUE\"]]\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get list of more general, semantically related labels from LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "parser = JsonOutputParser()\n",
    "prompt = ChatPromptTemplate(messages=[(\"system\",system_prompt),(\"human\", \"{input}\")],\n",
    "                                          partial_variables={\"format_instructions\": parser.get_format_instructions()})\n",
    "\n",
    "chain = prompt | llm | parser\n",
    "nodes_dict = chain.invoke({'input':node_labels})\n",
    "relation_dict = chain.invoke({'input':rel_labels})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Economy': ['Economy', 'Economic status'],\n",
       " 'List': ['List'],\n",
       " 'Index': ['Index'],\n",
       " 'Publication': ['Publication'],\n",
       " 'Concept': ['Concept'],\n",
       " 'Location': ['Location'],\n",
       " 'Person': ['Person'],\n",
       " 'Price': ['Price', 'Value'],\n",
       " 'Time': ['Time', 'Date'],\n",
       " 'Group': ['Group'],\n",
       " 'Event': ['Event'],\n",
       " 'Product': ['Product'],\n",
       " 'Company': ['Company', 'Organization', 'Industry'],\n",
       " 'License': ['License'],\n",
       " 'Stock': ['Stock'],\n",
       " 'Financial_metric': ['Financial_metric'],\n",
       " 'Url': ['Url']}"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nodes_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RECOVERED': ['RECOVERED', 'RECOVERED_FROM'],\n",
       " 'COMPONENTS_PRODUCED_IN': ['COMPONENTS_PRODUCED_IN'],\n",
       " 'MENTIONS': ['MENTIONS'],\n",
       " 'PRICE': ['OPENING_PRICE_132.76',\n",
       "  'LOWEST_PRICE',\n",
       "  'OPENING_PRICE_57.02',\n",
       "  'CLOSING_PRICE_134.18',\n",
       "  'OPENING_PRICE_76.07',\n",
       "  'CLOSING_PRICE_56.09',\n",
       "  'CLOSING_PRICE_80.01'],\n",
       " 'PART_OF': ['PART_OF'],\n",
       " 'MARKET_VALUE': ['MARKET_VALUE',\n",
       "  'MARKET_VALUE_HIGHER_THAN',\n",
       "  'MARKET_VALUE_RECORD',\n",
       "  'MARKET_VALUE_EXCEEDED'],\n",
       " 'FOUNDED_ON': ['FOUNDED_ON'],\n",
       " 'AUTHOR': ['AUTHOR'],\n",
       " 'IS': ['IS'],\n",
       " 'SURPASSED': ['SURPASSED_IN_BRAND_VALUE',\n",
       "  'SURPASSED_BY_APPLE_IN_BRAND_VALUE',\n",
       "  'SURPASSED'],\n",
       " 'TRANSACTION': ['HIGHER_TRANSACTION_NUMBER'],\n",
       " 'CAUSES': ['CAUSES_SHRINKAGE',\n",
       "  'INCREASE_CAUSES_STAGNATION',\n",
       "  'DECREASE_CAUSES_STAGNATION',\n",
       "  'DECLINE_CAUSES_STAGNATION'],\n",
       " 'DATA_OBSERVED_ON': ['DATA_OBSERVED_ON'],\n",
       " 'AFFECTED': ['AFFECTED_BY', 'AFFECTED'],\n",
       " 'HAS': ['HAS'],\n",
       " 'DISTRIBUTED_UNDER': ['DISTRIBUTED_UNDER'],\n",
       " 'COMPARISON': ['VOLUME_COMPARISON',\n",
       "  'STOCK_PRICE_FALL_COMPARISON',\n",
       "  'COMPARED_TO'],\n",
       " 'PROFIT': ['GREATER_PROFIT_MARGIN', 'GREATER_PROFIT'],\n",
       " 'RANK': ['FOURTH-LARGEST_BY_UNIT_SALES',\n",
       "  'FOURTH-LARGEST',\n",
       "  'LARGEST_BY_REVENUE',\n",
       "  'LARGEST_COMPANY_BY_MARKET_CAP',\n",
       "  'RANKED_6TH_ON'],\n",
       " 'STOCK': ['STOCK_PRICE_INCREASE',\n",
       "  'STOCK_PRICE_DROP',\n",
       "  'STOCK_PRICE_PEAK',\n",
       "  'STOCK_START_DECREASE'],\n",
       " 'PUBLISHED': ['PUBLISHED_BY', 'PUBLISHED_IN'],\n",
       " 'REPORTED_ON': ['REPORTED_ON'],\n",
       " 'LOCATED_IN': ['LOCATED_IN', 'HEADQUARTERED_IN'],\n",
       " 'FELL_OUT_OF_TRILLION_CLUB': ['FELL_OUT_OF_TRILLION_CLUB'],\n",
       " 'IMPACT': ['IMPACT', 'PREDICTED_IMPACT', 'IMPACTED', 'STAGNATION_IMPACTS'],\n",
       " 'DECLINE': ['DECLINE'],\n",
       " 'INCREASE': ['INCREASE'],\n",
       " 'RECOVERY': ['RECOVERY'],\n",
       " 'SIMILAR_TRADING_VOLUME': ['SIMILAR_TRADING_VOLUME'],\n",
       " 'STUDIED': ['STUDIED'],\n",
       " 'ANNOUNCED': ['ANNOUNCED', 'ANNOUNCEMENT_DATE'],\n",
       " 'MOST_VALUABLE_BRAND': ['MOST_VALUABLE_BRAND'],\n",
       " 'AFFILIATED_WITH': ['AFFILIATED_WITH'],\n",
       " 'PRODUCES': ['PRODUCES'],\n",
       " 'WENT_PUBLIC_ON': ['WENT_PUBLIC_ON'],\n",
       " 'REVENUE': ['GREATER_REVENUE'],\n",
       " 'HIGHEST_VALUE': ['HIGHEST_VALUE'],\n",
       " 'FOUNDED_BY': ['FOUNDED_BY'],\n",
       " 'EXPERIENCED_FLUCTUATION': ['EXPERIENCED_FLUCTUATION'],\n",
       " 'STATED_IN': ['STATED_IN'],\n",
       " 'PEAK': ['PEAK'],\n",
       " 'LOW': ['LOW'],\n",
       " 'RECORDED_IN': ['RECORDED_IN'],\n",
       " 'LARGEST_SINGLE_DAY_DROP_SINCE': ['LARGEST_SINGLE_DAY_DROP_SINCE'],\n",
       " 'START_DROP': ['START_DROP']}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "relation_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "node_match = {}\n",
    "relation_match = {}\n",
    "\n",
    "for new_label , values in nodes_dict.items() :\n",
    "    for old_label in values:\n",
    "        node_match[old_label]=new_label\n",
    "        \n",
    "for new_label , values in relation_dict.items() :\n",
    "    for old_label in values:\n",
    "        relation_match[old_label]=new_label        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Update new node labels to database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "for old_label, new_label in node_match.items():\n",
    "    graph.query(\"\"\"MATCH (n:$($label))\n",
    "                    SET n:$($new_label)\n",
    "                    REMOVE n:$($label)\n",
    "                    \"\"\",\n",
    "                    params={'label':old_label, 'new_label':new_label})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Update new relationship labels to database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "for old_label, new_label in relation_match.items():\n",
    "    graph.query(\"\"\"MATCH (n)-[r:$($label)]->(m)\n",
    "                    CREATE (n)-[r2:$($new_label)]->(m)\n",
    "                    WITH r\n",
    "                    DELETE r\n",
    "                    \"\"\",\n",
    "                    params={'label':old_label, 'new_label':new_label})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Todo -\n",
    "\n",
    "1. Query correction for relation updation and copying relation properties to new relation\n",
    "2. Sending nodes and relation labels in batches to LLM if exceed X number.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
